﻿using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using Veralkohol.Commons;

namespace Veralkohol.ViewModel
{
    public class OpenViewModel : VMAbstract
    {
        public ObservableCollection<SavedData> Files { get; set; }

        public OpenViewModel()
        {
            Files = new ObservableCollection<SavedData>();
            try
            {
                foreach (var item in File.ReadAllLines("server.saves"))
                {
                    var sor = item.Split(new[] { '%' });
                    Files.Add(new SavedData() { date = sor[0], guid = new Guid(sor[1]) });
                }
            }
            catch { }
        }

        public static Guid RemoteGuid = new Guid();

        RelayCommand cancel;
        public RelayCommand Cancel
        {
            get
            {
                return NewRCommand(cancel,
                    () =>
                    {
                        App.Current.Windows[1].Close();
                    }
                );
            }
        }

        RelayCommand open;
        public RelayCommand Open
        {
            get
            {
                return NewRCommand(open,
                    () =>
                    {
                        
                    }
                );
            }
        }

        RelayCommand<SelectionChangedEventArgs> selchanged;
        public RelayCommand<SelectionChangedEventArgs> SelectionChanged
        {
            get
            {
                return NewRCommand<SelectionChangedEventArgs>(selchanged,
                    (param) =>
                    {
                        RemoteGuid = (param.AddedItems[0] as SavedData).guid;
                    }
                );
            }
        }
    }


    public class SavedData
    {
        public string date { get; set; }
        public Guid guid { get; set; }
    }
}
